package com.ak47.cms.cms.kong

import com.ak47.cms.cms.builder.CrawlerWebClient
import com.ak47.cms.cms.dao.KongShopRepository
import com.ak47.cms.cms.entity.KongShop
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * @author: Jack
 * 2020/11/8 01:05
 */

@Service
class ShopCollector {
    @Autowired
    lateinit var kongShopRepository: KongShopRepository

    val log = LoggerFactory.getLogger(this::class.java)


    /**
     * 店铺主页url
     */
    fun shopUrl(shopId: String) = "http://shop.kongfz.com/${shopId}"

    /**
     * 当前页店铺的列表
     */
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?event=sale&type=2&provId=31000000000&cityId=0&currPage=${page}&async=1&_=1604765315642"
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?event=sale&type=4&provId=31000000000&cityId=0&currPage=$page&async=1&_=1604771867868"
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?event=sale&type=3&provId=31000000000&cityId=0&currPage=$page&async=1&_=1604772479314"
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?event=tan&type=3&provId=31000000000&cityId=0&currPage=$page&async=1&_=1604773142890"
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?event=area&type=3&provId=31000000000&cityId=0&currPage=$page&async=1&_=1604773186287"
//    fun shopPageUrl(page: Int) = "http://shop.kongfz.com/ranklist/?type=3&provId=31000000000&cityId=0&event=tanarea&currPage=$page&async=1&_=1604773186323"

//    fun shopPageMap(): Map<Int, String> {
//        return mapOf(
//            162 to "http://shop.kongfz.com/ranklist/?type=3&provId=1000000000&event=area&async=1&_=1604773186323&currPage=",
//            42 to "http://shop.kongfz.com/ranklist/?type=3&event=area&provId=2000000000&async=1&_=1604773186323&currPage=",
//            64 to "http://shop.kongfz.com/ranklist/?type=3&event=area&provId=12000000000&async=1&_=1604773186323&currPage=",
//            57 to "http://shop.kongfz.com/ranklist/?type=3&event=area&provId=18000000000&async=1&_=1604773186323&currPage=",
//            60 to "http://shop.kongfz.com/ranklist/?type=3&event=area&provId=24000000000&async=1&_=1604773186323&currPage=",
//            31 to "http://shop.kongfz.com/ranklist/?type=3&event=area&provId=31000000000&async=1&_=1604773186323&currPage="
//        )
//    }

    fun shopPageMap(): Map<Int, String> {
        return mapOf(
            47 to "http://shop.kongfz.com/ranklist/?event=cat&type=8000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            14 to "http://shop.kongfz.com/ranklist/?event=cat&type=9000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            11 to "http://shop.kongfz.com/ranklist/?event=cat&type=21000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            28 to "http://shop.kongfz.com/ranklist/?event=cat&type=37000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            15 to "http://shop.kongfz.com/ranklist/?event=cat&type=6000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            16 to "http://shop.kongfz.com/ranklist/?event=cat&type=43000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            30 to "http://shop.kongfz.com/ranklist/?event=cat&type=3000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            40 to "http://shop.kongfz.com/ranklist/?event=cat&type=4000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            2 to "http://shop.kongfz.com/ranklist/?event=cat&type=31000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            343 to "http://shop.kongfz.com/ranklist/?event=cat&type=20000000000000000&provId=31000000000&cityId=0&async=1&_=1604774850311&currPage=",
            473 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=1000000000&async=1&_=1604774850311&currPage=",
            230 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=2000000000&async=1&_=1604774850311&currPage=",
            98 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=3000000000&async=1&_=1604774850311&currPage=",
            57 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=4000000000&async=1&_=1604774850311&currPage=",
            94 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=5000000000&async=1&_=1604774850311&currPage=",
            82 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=6000000000&async=1&_=1604774850311&currPage=",
            37 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=7000000000&async=1&_=1604774850311&currPage=",
            282 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=8000000000&async=1&_=1604774850311&currPage=",
            45 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=9000000000&async=1&_=1604774850311&currPage=",
            27 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=10000000000&async=1&_=1604774850311&currPage=",
            9 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=11000000000&async=1&_=1604774850311&currPage=",
            170 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=12000000000&async=1&_=1604774850311&currPage=",
            170 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=13000000000&async=1&_=1604774850311&currPage=",
            74 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=14000000000&async=1&_=1604774850311&currPage=",
            117 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=15000000000&async=1&_=1604774850311&currPage=",
            84 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=16000000000&async=1&_=1604774850311&currPage=",
            76 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=17000000000&async=1&_=1604774850311&currPage=",
            261 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=18000000000&async=1&_=1604774850311&currPage=",
            57 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=19000000000&async=1&_=1604774850311&currPage=",
            160 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=20000000000&async=1&_=1604774850311&currPage=",
            40 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=21000000000&async=1&_=1604774850311&currPage=",
            9 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=22000000000&async=1&_=1604774850311&currPage=",
            4 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=23000000000&async=1&_=1604774850311&currPage=",
            291 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=24000000000&async=1&_=1604774850311&currPage=",
            93 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=25000000000&async=1&_=1604774850311&currPage=",
            92 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=26000000000&async=1&_=1604774850311&currPage=",
            129 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=27000000000&async=1&_=1604774850311&currPage=",
            2 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=28000000000&async=1&_=1604774850311&currPage=",
            1 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=29000000000&async=1&_=1604774850311&currPage=",
            39 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=30000000000&async=1&_=1604774850311&currPage=",
            182 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=tanarea&provId=31000000000&async=1&_=1604774850311&currPage=",
            162 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1000000000&async=1&_=1604835974525&currPage=",
            42 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2000000000&async=1&_=1604835974525&currPage=",
            20 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=3000000000&async=1&_=1604835974525&currPage=",
            12 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=4000000000&async=1&_=1604835974525&currPage=",
            24 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=5000000000&async=1&_=1604835974525&currPage=",
            15 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=6000000000&async=1&_=1604835974525&currPage=",
            10 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=7000000000&async=1&_=1604835974525&currPage=",
            43 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=8000000000&async=1&_=1604835974525&currPage=",
            10 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=9000000000&async=1&_=1604835974525&currPage=",
            4 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=10000000000&async=1&_=1604835974525&currPage=",
            2 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1100000000&async=1&_=1604835974525&currPage=",
            64 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1200000000&async=1&_=1604835974525&currPage=",
            42 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1300000000&async=1&_=1604835974525&currPage=",
            17 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1400000000&async=1&_=1604835974525&currPage=",
            29 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1500000000&async=1&_=1604835974525&currPage=",
            20 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1600000000&async=1&_=1604835974525&currPage=",
            17 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1700000000&async=1&_=1604835974525&currPage=",
            57 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1800000000&async=1&_=1604835974525&currPage=",
            15 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=1900000000&async=1&_=1604835974525&currPage=",
            36 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2000000000&async=1&_=1604835974525&currPage=",
            8 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2100000000&async=1&_=1604835974525&currPage=",
            2 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2200000000&async=1&_=1604835974525&currPage=",
            1 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2300000000&async=1&_=1604835974525&currPage=",
            60 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2400000000&async=1&_=1604835974525&currPage=",
            19 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2500000000&async=1&_=1604835974525&currPage=",
            19 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2600000000&async=1&_=1604835974525&currPage=",
            26 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=2700000000&async=1&_=1604835974525&currPage=",
            10 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=3000000000&async=1&_=1604835974525&currPage=",
            31 to "http://shop.kongfz.com/ranklist/?type=8000000000000000&event=area&provId=3100000000&async=1&_=1604835974525&currPage="
        )
    }

    /**
     * 先把所有的店铺ID拿到,存到数据库里
     */
    fun getShopIds() {

        val crawlerWebClient = CrawlerWebClient()

        for ((k, v) in shopPageMap()) {
            val pages = k
            (1..k).forEach {
                val pageIndex = it

                val shopPageurl = "$v$pageIndex"

                val pageJson: String = crawlerWebClient.getJson(shopPageurl)
                val jsonObject = JSON.parseObject(pageJson)
                val shopList = (jsonObject.get("result") as JSONObject).get("list") as JSONArray
                shopList.forEach {
                    val shop = it as JSONObject
                    val shopId = shop["shopId"]
                    println(shopId)
                    val kongShop = KongShop()
                    kongShop.shopId = shopId.toString()

                    try {
                        kongShopRepository.save(kongShop)
                    } catch (e: Exception) {
                        log.info("shopId:{}", shopId, e)
                    }
                }
            }


        }


    }


    /**
     * 先打开浏览器client，登陆我的“孔夫子旧书网店铺”， 根据shopId打开书店主页：http://shop.kongfz.com/${shopId}/，
    浏览器open url：
    http://shop.kongfz.com/widget/ajax?widget=shopInfo&tpl=store&api=collect&shopId=110&_=1604766090357
    即可（collect关注的请求是幂等的，重复关注，依然是关注）。
    这个需要用到 chromedriver + Selenium .
     */



}


